<?php
class ControllerDelimanBaocaotinhhinhphathanhbaotaikhuvucquanly extends Controller
{
	private $error = array();
   	function __construct() 
	{
		$listchek = $this->getListCheck();
		
		if(in_array($this->getMethod(),$listchek))
		{
			if(!$this->user->hasPermission($this->getRoute(), $this->getMethod()))
			{
				$this->response->redirect("?route=common/permission");
			}
		}
		
		$this->load->model("deliman/loaibao");
		$this->load->model("deliman/kybao");
	 	$this->load->model("deliman/phieuphanphoi");
	 	$this->load->model("deliman/khachhang");
		$this->load->model('deliman/nhanvien');
		$this->load->model('deliman/khuvuc');
		$this->load->model("common/control");
   	}
	
	public function index()
	{
		$this->getList();
	}
		
	private function getList() 
	{		
		//$data = $this->request->get;
		$this->data['phieuphanphoiid'] = null;
		$this->data['khuvucs'] = array();
				
		// Danh sach LOAI BAO co khach hang da dat		
		$where = "";	
		
		$this->data['dsloaibao']=array();// COMBOBOX LOAI BAO
		$this->data['dsloaibao'] = $this->model_deliman_loaibao->getList($where);
		
		// Load TREE
		$ctphanphoi = array();
		if ((isset($this->request->get['kybaoid'])) ) 
		{
			// Lay thong tin phieuphanphoi
      		$this->data['item'] = $this->model_deliman_phieuphanphoi->getList(" AND kybaoid='".$this->request->get['kybaoid']."'");
			// Dung de kiem tra xem: ky bao nay DUOC phan phoi chua
			$this->data['phieuphanphoiid'] = $this->data['item'][0]['phieuphanphoiid'];
			//Lay thong tin CHI TIET phieu phan phoi
			$where = " AND phieuphanphoiid = '". $this->data['item'][0]['phieuphanphoiid'] ."'";
			$ctphanphoi = $this->model_deliman_phieuphanphoi->getChiTietPhieuPhanPhoiList($where);
			
			$arrKhuVucTree = array();
			//$this->model_deliman_khuvuc->getTree(0,$arrKhuVucTree);
			
			// Lay danh sach khu vuc ma NHAN VIEN dang nhap QL
			// Lay ds khuvucid của các khu vực mà nhân viên đang đăng nhập quản lý
			$dskhuvucphutrachid = array();
			// Lay nhung KHU VUC ma nhan vien QL
			if(trim($this->user->getNhanVien('loainhanvienid')) == '2')
			{
				$dskhuvucphutrachid = $this->model_deliman_nhanvien->getPhuTrachKhuVucList(" AND nhanvienphutrachid ='".$this->user->getNhanVien('nhanvienid')."' AND tinhtrang != 'deleted' ORDER BY khuvucid ASC");
				$dskhuvucphutrachid = $this->string->matrixToArray($dskhuvucphutrachid, 'khuvucid');
				$dskhuvucphutrachid = array_unique($dskhuvucphutrachid);
			}
			else
			{
				$dskhuvucphutrachid[]='0';
			}
			
			if(count($dskhuvucphutrachid)>0)
			{
				foreach($dskhuvucphutrachid as $kvid)
				{
					$khuvus = array();
					$this->model_deliman_khuvuc->getTree($kvid, $khuvus,$kvid);
					$arrKhuVucTree = array_merge($arrKhuVucTree, $khuvus);
				}
			}
			
			$eid="ex-node";
			$eclass="child-of-ex-node";
			
			foreach($arrKhuVucTree as $item )
			{	
				
				$deep = $item['level'];	
				$tab="";
				
				if(count($item['countchild'])==0)
					 $tab="<span class='tab'></span>";
			
				$class="";
				
				if($item['parentpath']!="")
					$class=$eclass.$item['parentpath'];
				
				$row = $this->string->array_Filter($ctphanphoi,'khuvucid',$item['khuvucid']);
				$thucGiao = 0;
				$thucGiao = $this->tinhThucGiaoTheoKhuVuc($item['khuvucid'], $this->request->get['kybaoid']);
				$this->data["khuvucs"][]=array(
											
											'ctphanphoi' =>$row[0],
											'khuvucid'=>$item['khuvucid'],
											'thucgiao'=>$thucGiao,
											'prefix'=>$this->string->getPrefix("--",$deep),
											'deep'=>$deep + 1,
											'eid' =>$eid . $item['path'] ,
											'class' =>$class,
											'tenkhuvuc'=>$item['tenkhuvuc'],
											'khuvuccha'=>$item['khuvuccha'],
											'position'=>$item['position'],
											'tab'=>$tab
											
										);
			}
    	}
				
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="deliman/baocaotinhhinhphathanhbaotaikhuvucquanly.tpl";
		$this->layout="layout/deliman";
		
		$this->render();
	}
	
	// Post ds ky bao theo loai bao da chon vao combobox
	public function getKyBao()
	{
		$where = "";
		$loaibaoid =  $this->request->get['loaibaoid'];
		$tungay =  $this->request->get['tungay'];
		$denngay =  $this->request->get['denngay'];
		$select =  $this->request->get['select'];
		$where .= " AND loaibaoid = '".$loaibaoid."'";
		if(trim($tungay)!='')
		{
			$where .= " AND ngayphathanh >= '".date('Y-m-d',strtotime($tungay))."'";
		}
		if(trim($denngay)!='')
		{	
			$where .= " AND ngayphathanh <= '".date('Y-m-d',strtotime($denngay))." 24:00:00'";
		}
		$cmbkybao = $this->model_deliman_kybao->getList($where." ORDER BY ngayphathanh DESC");
		$this->data['output'] = $this->model_common_control->getDataCombobox($cmbkybao, "tenkybao", "kybaoid", $select);
		
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function tinhThucGiaoTheoKhuVuc($khuvucid, $kybaoid)
	{
		$dskhuvucid = array();
		$this->model_deliman_khuvuc->getTree($khuvucid, $dskhuvucid);
		$dskhuvucid = $this->string->matrixToArray($dskhuvucid,'khuvucid');
		$dskhuvucid = array_unique($dskhuvucid);
		//Lay ds nhung KHACH HANG trong khu vuc do
		$dskhachhangid = array();
		$dskhachhangid = $this->model_deliman_khachhang->getList(" AND khuvucid in( '".implode("','",$dskhuvucid)."')");
		$dskhachhangid = $this->string->matrixToArray($dskhachhangid,'khachhangid');
		// Tao dk: tinh Thuc Giao theo khu vuc id
		$where .= " AND kybaoid = '".$kybaoid."' AND khachhangid in( '".implode("','",$dskhachhangid)."')";
		$dskhachhangdatbao=array();
		$dskhachhangdatbao = $this->model_deliman_khachhang->getThongTinDatBaoList($where);	
		$thucGiao = 0;
		foreach($dskhachhangdatbao as $khdb)
		{
			if(isset($khdb['soluongdagiao']))
				$thucGiao += $khdb['soluongdagiao'];
		}
		return $thucGiao;
	}
}
?>